<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Menu_Edit extends MY_Controller
{

    /**
     * Index Page for this controller.
     *
     * Maps to the following URL
     *        http://example.com/index.php/welcome
     *    - or -
     *        http://example.com/index.php/welcome/index
     *    - or -
     * Since this controller is set as the default controller in
     * config/routes.php, it's displayed at http://example.com/
     *
     * So any other public methods not prefixed with an underscore will
     * map to /index.php/welcome/<method_name>
     * @see https://codeigniter.com/user_guide/general/urls.html
     */
    public $cfg_file = './assets/config/sidebar_menu.json';
    public $menus;

    public function __construct()
    {
        parent::__construct();
        $this->auth('admin');
        $this->_add_style_sheet("dataTables", base_url("assets/DataTables/css/dataTables.bootstrap4.min.css"));
        $this->_add_script_file("dataTables", base_url("assets/DataTables/js/jquery.dataTables.min.js"));
        $this->_add_script_file("dataTables Bootstrap", base_url("assets/DataTables/js/dataTables.bootstrap.min.js"));

        //$this->_add_style_sheet("jQueryUI", base_url("assets/jQueryUI-1.12.1/jquery-ui.min.css"));
        //$this->_add_script_file("jQueryUI", base_url("assets/jQueryUI-1.12.1/jquery-ui.js"));

        $this->menus = json_decode(file_get_contents($this->cfg_file), true);
    }

    public function index()
    {
        $data = array();
        $this->_view('admin/menu_edit.twig', $data,'admin','admin/menu_edit');
    }

    public function json()
    {
        $json = array();
        $json["data"] = $this->menus;
        print_r(json_encode($json));
    }

    public function add()
    {
        $data = array();
        if ($this->input->post()) {
            $maxid = 0;
            foreach ($this->menus as $key => $value) {
                $maxid = max($maxid, $value ['id']);
            }
            //生成post item
            $menu_item = array(
                'id' => $maxid + 1,
                'title' => $this->input->post('name'),
                'route' => $this->input->post('route') ?: '/',
                'class' => $this->input->post('class') ?: 'href',
                'parent' => $this->input->post('parent') ?: 0,
                'order' => $this->input->post('order') ?: 0
            );
            if (!empty($menu_item['title'])) {
                array_push($this->menus, $menu_item);
                $this->update();
                echo '<div class="alert alert-success alert-dismissible"><i class="icon fa fa-check"></i>菜单添加成功！</div>';
            } else {
                echo '<div class="alert alert-danger alert-dismissible"><i class="icon fa fa-ban"></i>菜单名不能为空!</div>';
            }
        } else {
            $this->_view('admin/menu_edit-add.twig', $data);
        }
    }

    public function edit($id = '')
    {
        $data = array();
        foreach ($this->menus as $key => $value) {
            if ($value ['id'] == $id) {
                $index = $key;
                $data["input"] = $value;
            }
        }
        if (empty($data['input'])) {
            die('<div class="alert alert-danger alert-dismissible"><i class="icon fa fa-ban"></i>菜单项不存在!</div>');
        }
        if ($this->input->post()) {
            //生成post item
            $menu_item = array(
                'id' => $id,
                'title' => $this->input->post('name'),
                'route' => $this->input->post('route') ?: '/',
                'class' => $this->input->post('class') ?: 'href',
                'parent' => $this->input->post('parent') ?: 0,
                'order' => $this->input->post('order') ?: 0
            );
            if (!empty($menu_item['title'])) {
                $this->menus[$index] = $menu_item;
                $this->update();
                exit('<div class="alert alert-success alert-dismissible"><i class="icon fa fa-check"></i>修改成功！</div>');
            } else {
                die('<div class="alert alert-danger alert-dismissible"><i class="icon fa fa-ban"></i>菜单名不能为空!</div>');
            }
        } else {
            $this->_view('admin/menu_edit-edit.twig', $data);
        }
    }

    public function del($id = '')
    {
        $data = array();
        foreach ($this->menus as $key => $value) {
            if ($value ['id'] == $id) {
                $index = $key;
                $data["input"] = $value;
            }
        }
        if (empty($data['input'])) {
            die('<div class="alert alert-danger alert-dismissible"><i class="icon fa fa-ban"></i>菜单项不存在!</div>');
        }
        if ($this->input->post()) {
            array_splice($this->menus, $index, 1);
            file_put_contents($this->cfg_file, json_encode($this->menus));
            exit('删除成功！');
        } else {
            $this->_view('admin/menu_edit-del.twig', $data);
        }
    }

    public function update()
    {
        foreach ($this->menus as $key => $value) {
            $m_parent[$key] = $value ['parent'];
            $m_order[$key] = $value ['order'];
        }
        //排序 依法储存
        array_multisort($m_parent, SORT_ASC, $m_order, SORT_ASC, $this->menus);
        // 写入json配置文件
        file_put_contents($this->cfg_file, json_encode($this->menus));
        return true;
    }
}
